package com.sky.web.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //分类分页查询
    @GetMapping("/page")
    public Result findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageBean pageBean = categoryService.findByPage(categoryPageQueryDTO);
        return Result.success(pageBean);
    }

    //新增分类
    @PostMapping
    public Result save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();
    }

    //删除分类-->根据主键删除
    @DeleteMapping
    public Result deleteById(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    //修改分类
    @PutMapping
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }

    //启用.禁用分类
    @PostMapping("/status/{status}")
    public Result startOrstop(@PathVariable("status") Integer status, Long id) {
        //1.构建category对象
        Category category =Category.builder()
                .status(status)
                .id(id)
                .build();
        //2. 调用service更新,直接复用update
        categoryService.update(category);
        //3.返回结果
        return Result.success();
    }

    //根据类型查询分类
    @GetMapping("/list")
    public Result findByStatusAndType(Integer type) {
        return Result.success(categoryService.findByStatusAndType(type));
    }

}
